package com.hc.desaes;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Base64;

/**
 * @author : Henry
 * {@code CreateTime}: 2025-08-25 23:07
 * {@code Version}: 1.0
 */
public class AesDemo {
    public static void main(String[] args) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
        // 原文
        String input = "硅谷";
        // 定义key
        // DES加密，密钥长度必须是8字节
        // AES加密，密钥长度必须是16字节
        String key = "1234567812345678";
        // 算法
        String transformation = "AES";
        // 加密类型
        String algorithm = "AES";
        String encryptDES = encryptDES(transformation, key, algorithm, input);
        System.out.println("加密：" + encryptDES);
        String s = decryptDES(encryptDES, key, transformation, algorithm);
        System.out.println("解密：" + s);
    }

    /**
     * 解密
     *
     * @param input          密文
     * @param key            密钥
     * @param transformation 算法
     * @param algorithm      解密类型
     * @return 原文
     * @throws NoSuchPaddingException
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     * @throws IllegalBlockSizeException
     * @throws BadPaddingException
     */
    private static String decryptDES(String input, String key, String transformation, String algorithm) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
        Cipher cipher = Cipher.getInstance(transformation);
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), algorithm);
        // Cipher.DECRYPT_MODE: 解密
        // 解密规则
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
        byte[] decode = Base64.getDecoder().decode(input.getBytes());
        byte[] bytes = cipher.doFinal(decode);
        return new String(bytes);
    }

    /**
     * 加密
     *
     * @param transformation 算法
     * @param key            密钥
     * @param algorithm      加密类型
     * @param input          原文
     * @return 密文
     * @throws NoSuchAlgorithmException
     * @throws NoSuchPaddingException
     * @throws InvalidKeyException
     * @throws IllegalBlockSizeException
     * @throws BadPaddingException
     */
    private static String encryptDES(String transformation, String key, String algorithm, String input) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
        // 创建加密对象
        Cipher cipher = Cipher.getInstance(transformation);
        // 创建加密规则
        // 第一个参数：表示key的字节
        // 第二个参数：表示加密的类型
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), algorithm);
        // 进行加密初始化
        // 参数1：模式，加密模式，解密模式
        // 参数2：加密的规则
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        // 调研加密方法
        // 参数：原文的字节数组
        byte[] bytes = cipher.doFinal(input.getBytes());
        System.out.println(Arrays.toString(bytes));
//        // 打印密文：乱码
//        System.out.println(new String(bytes));
        // 场景base64对象
        byte[] encode = Base64.getEncoder().encode(bytes);
        return new String(encode);
    }
}
